<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Chat
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: Whispers.php 7244 2010-09-01 01:49:53Z john $
 * @author     John
 */

/**
 * @category   Application_Extensions
 * @package    Chat
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @author     John
 */
class Advchat_Model_DbTable_Histories extends Engine_Db_Table
{
  protected $_rowClass = 'Advchat_Model_History';

  public function getLastDateChat($user_id,$recipient_id =0,$open = 1 ){
     $select = $this->select()
                    ->where('owner_id = ?',$user_id)
                    ->where('openconversation = ?',$open)
                    //->where('owner_id not in(select recipient_id from engine4_advchat_histories where owner_id = ? and openconversation =0)',$user_id)
                    ->order('date desc');
     if(!empty($recipient_id)){
        $select->where('recipient_id = ?',$recipient_id);
     }
     $rows = $this->fetchAll($select);
     
     $result = array();
     foreach($rows as $row){
        $result[$row->recipient_id] = strtotime($row->olddate);
     }
     return $result;
                     
  }
  public function deleteWhisper($user_id,$recipient_id){
    if(empty($recipient_id)) return ;
   $select = $this->select()
                   ->where('owner_id = ?',$user_id)
                   ->orWhere('owner_id = ?',$recipient_id)
                   ->order('date desc');
    $row = $this->fetchRow($select);
    if($row)$min =  strtotime($row->date);
    else $min = 0;
    
    $whisperT = Engine_Api::_()->getDbtable('whispers','advchat');
    $where = " (sender_id = $user_id or sender_id = $recipient_id) and date <= $min ";
    $whisperT->delete($where);
                         
  }
  public function getAllConversations($user_id){
    $select = $this->select()
                    ->where('owner_id = ?',$user_id)
                    ->where('openconversation = 1')
                    ->where('owner_id not in(select recipient_id from engine4_advchat_histories where owner_id = ? and openconversation =0)',$user_id)
                    ->order('date desc');
     $rows = $this->fetchAll($select); 
     $result = array();
     foreach($rows as $row){
        $result[] = array('recipient_id' => $row->recipient_id,'conversation_id'=>$row->conversation_id);
     }
     return $result;
  }
  
  
  public function closeConversation($user_id,$recipient_id){
        $select = $this->select()
                     ->where('owner_id = ?',$user_id)
                     ->where('recipient_id = ?',$recipient_id);
       $row = $this->fetchRow($select);
       if($row){
        $row->openconversation = 0;
        $row->save();
       }
  }
  public function insertHistory($user_id,$recipient_id,$conversation = 0,$updatedate = false){
      $view  = Zend_Registry::get('Zend_View');  
      $select = $this->select()
                     ->where('owner_id = ?',$user_id)
                     ->where('recipient_id = ?',$recipient_id);
      $row = $this->fetchRow($select);
      if($row){
         if($updatedate && $row->date != date('Y-m-d')){
            $row->olddate =  $row->date;
            $row->date = date('Y-m-d');
         }
         
        $row->openconversation = 1;
        $row->save();
         
         
      }
      else {
        $user = Engine_Api::_()->getItem('user',$user_id);
        $recipient = Engine_Api::_()->getItem('user',$recipient_id);
        $conversation = $this->getConversation($user_id,$recipient_id);
        if(!$conversation){
            $conversation = Engine_Api::_()->getItemTable('messages_conversation')->send(
                    $user,
                    array($recipient_id),
                    $view->translate('Between ').$recipient->username." and ".$user->username,
                    ''
            );
        }
        $row = $this->createRow();
        $row->owner_id = $user_id;
        $row->recipient_id = $recipient_id;
        $row->date = date('Y-m-d');
        $row->olddate = date('Y-m-d');
        $row->conversation_id = $conversation->conversation_id;
        $row->save();
        
        $row = $this->createRow();
        $row->owner_id = $recipient_id;
        $row->recipient_id = $user_id;
        $row->date = date('Y-m-d');
        $row->olddate = date('Y-m-d');
        $row->conversation_id = $conversation->conversation_id;
        $row->save();
        
      }
     return $row;                 
  }
  
  public function getConversation($user_id,$recipient_id){
       $select = $this->select()
                     ->where("(owner_id = $user_id and recipient_id = $recipient_id) or(owner_id = $recipient_id and recipient_id =$user_id)")
                     ->where('conversation_id <> 0');
               
       $row = $this->fetchRow($select);
       return $row;
  }
}